<?php
/**
 * @package H.Thuong
 */
/*
Plugin Name: H.Thuong
Plugin URI: http://www.thuongnguyen.net/
Description: H.Thuong 
Version: 1.0
Author: H.Thuong
Author URI: http://www.thuongnguyen.net/
*/

function register() {
	//print_r(get_user_meta(get_current_user_id()));
	if (isset($_POST) && $_POST['postType'] == 'register') {
		check_ajax_referer( 'keyForRegister', 'sid' );
		$user_id = username_exists($_POST['email']);
		if ( !$user_id and email_exists($_POST['email']) == false ) {
			$user_id = wp_create_user($_POST['email'], $_POST['password'], $_POST['email']);
			if (isset($_POST['tendoanhnghiep']) && !empty($_POST['tendoanhnghiep'])) {
				add_user_meta($user_id, 'tendoanhnghiep', $_POST['tendoanhnghiep']);
			}
			if (isset($_POST['thanhpho']) && !empty($_POST['thanhpho'])) {
				add_user_meta($user_id, 'thanhpho', $_POST['thanhpho']);
			}
			if (isset($_POST['diachi']) && !empty($_POST['diachi'])) {
				add_user_meta($user_id, 'diachi', $_POST['diachi']);
			}
			if (isset($_POST['dienthoai']) && !empty($_POST['dienthoai'])) {
				add_user_meta($user_id, 'dienthoai', $_POST['dienthoai']);
			}
			if (isset($_POST['linhvuckinhdoanh']) && !empty($_POST['linhvuckinhdoanh'])) {
				add_user_meta($user_id, 'linhvuckinhdoanh', $_POST['linhvuckinhdoanh']);
			}
			if (isset($_POST['quymo']) && !empty($_POST['quymo'])) {
				add_user_meta($user_id, 'quymo', $_POST['quymo']);
			}
			if (isset($_POST['website']) && !empty($_POST['website'])) {
				add_user_meta($user_id, 'website', $_POST['website']);
			}
			if (isset($_POST['facebook']) && !empty($_POST['facebook'])) {
				add_user_meta($user_id, 'facebook', $_POST['facebook']);
			}
			if (isset($_POST['gioithieu']) && !empty($_POST['gioithieu'])) {
				add_user_meta($user_id, 'gioithieu', $_POST['gioithieu']);
			}
			echo 'success';
		} else {
			echo 'emailExists';
		}
		exit;
	}
	if (isset($_POST) && $_POST['postType'] == 'update') {
		check_ajax_referer( 'keyForUpdate', 'sid' );
		$user_id = get_current_user_id();
		if (isset($_POST['tendoanhnghiep']) && !empty($_POST['tendoanhnghiep'])) {
			update_user_meta($user_id, 'tendoanhnghiep', $_POST['tendoanhnghiep']);
		}
		if (isset($_POST['thanhpho']) && !empty($_POST['thanhpho'])) {
			update_user_meta($user_id, 'thanhpho', $_POST['thanhpho']);
		}
		if (isset($_POST['diachi']) && !empty($_POST['diachi'])) {
			update_user_meta($user_id, 'diachi', $_POST['diachi']);
		}
		if (isset($_POST['dienthoai']) && !empty($_POST['dienthoai'])) {
			update_user_meta($user_id, 'dienthoai', $_POST['dienthoai']);
		}
		if (isset($_POST['linhvuckinhdoanh']) && !empty($_POST['linhvuckinhdoanh'])) {
			update_user_meta($user_id, 'linhvuckinhdoanh', $_POST['linhvuckinhdoanh']);
		}
		if (isset($_POST['quymo']) && !empty($_POST['quymo'])) {
			update_user_meta($user_id, 'quymo', $_POST['quymo']);
		}
		if (isset($_POST['website']) && !empty($_POST['website'])) {
			update_user_meta($user_id, 'website', $_POST['website']);
		}
		if (isset($_POST['facebook']) && !empty($_POST['facebook'])) {
			update_user_meta($user_id, 'facebook', $_POST['facebook']);
		}
		if (isset($_POST['gioithieu']) && !empty($_POST['gioithieu'])) {
			update_user_meta($user_id, 'gioithieu', $_POST['gioithieu']);
		}
		echo 'success';
		exit;
	}
	
}
add_action( 'wp', 'register' );